﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using iNet.Media.Imaging.Exif;
using iNet.Tasks;

namespace iNet.Media.Imaging
{
	/// <summary>
	/// ORF 影像解碼工作。
	/// </summary>
	public class OrfImageDecodeTask : CameraRawImageDecodeTask
	{
		#region 建構子
		/// <summary>
		/// 初始化新的 OrfImageDecodeTask 執行個體。
		/// </summary>
		public OrfImageDecodeTask()
			: base(ImageFormats.Orf)
		{ }
		/// <summary>
		/// 初始化新的 OrfImageDecodeTask 執行個體。
		/// </summary>
		/// <param name="source">影像儲存來源。</param>
		public OrfImageDecodeTask(MediaStorage source)
			: base(ImageFormats.Orf, source, 0, 0)
		{ }
		#endregion
		#region DecodeMetadataOverride
		internal override IMetadata DecodeMetadataOverride(Stream stream, bool isReadOnly)
		{
			return new ExifImageMetadata(new OrfReader(stream).ReadAllTags(), isReadOnly);
		}
		#endregion
		#region DecodeThumbnailImageOrientationOverride
		internal override ImageOrientation? DecodeThumbnailImageOrientationOverride(TaskVersion version, Stream stream)
		{
			OrfReader orfReader = new OrfReader(stream);
			while (orfReader.ReadNextTag())
			{
				if (orfReader.Ifd == Ifd.Ifd0 && orfReader.TagId == 274 && orfReader.TagType == ExifTagType.UInt16)
				{
					ExifTag tag = orfReader.Tag;
					if (tag != null && tag.Count == 1 && tag.Data != null && tag.Data.Length == 2)
						return (ImageOrientation)(int)tag.BitConverter.ToUInt16(tag.Data);
				}
			}
			return null;
		}
		#endregion
		#region FreeImageBrightnessAdjustment
		internal override int FreeImageBrightnessAdjustment
		{
			get
			{
				return 30;
			}
		}
		#endregion
	}
}
